package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author stone
 * @date 2021/11/23 14:26
 */
@RestController
@RequestMapping("other")
public class OtherParameterController {

    @RequestMapping("reqAndResp")
    public BaseRespVo reqAndResp(HttpServletRequest request,
                                 HttpServletResponse response) {
        //JavaEE做doGet或doPost是类似
        return BaseRespVo.ok(null);
    }

    @RequestMapping("cookie")
    public BaseRespVo cookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName() + ":" + cookie.getValue());
        }
        return BaseRespVo.ok(null);
    }
    @RequestMapping("session1")
    public BaseRespVo session1(HttpServletRequest request,String username){
        HttpSession session = request.getSession();
        session.setAttribute("username",username);
        return BaseRespVo.ok(null);
    }
    @RequestMapping("session2")
    public BaseRespVo session2(HttpSession session){
        Object username = session.getAttribute("username");
        return BaseRespVo.ok(username);
    }
}
